package NetOperation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import zOthers.Para;

public class SimpleRequest {

	// !! Important!!
	// Need to make this class Runnable

	private static Socket socket;
	private static PrintWriter out;
	private static BufferedReader in;

	/**
	 * Setup a connection between server and client
	 */
	public static void zInit(){
		//Create socket connection
		try{
			socket = new Socket(Para.serverIP, 1228);
			out = new PrintWriter(socket.getOutputStream(),
					true);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
		} catch (UnknownHostException e) {
			System.out.println("Unknown host: "+Para.serverIP);
			System.exit(1);
		} catch  (IOException e) {
			System.out.println("No I/O");
			System.exit(1);
		}

		// Connection is established if server receives this true.
		out.println(true);

	}

	/**
	 * Sends a String request to server
	 * @param rq
	 */
	public static void request(String rq){

		// Send a request to server
		out.println(rq);

		try{
			// Receives from server
			System.out.println(in.readLine());
		}catch(Exception e){}

	}

	/**
	 * Sends a int request to server
	 * @param rq
	 */
	public static void request(int rq){

		// Send a request to server
		out.println(rq);

		try{
			// Receives from server
			System.out.println(in.readLine());
		}catch(Exception e){}

	}

}
